/**
 * 
 */
package pl.edu.agh.winmon.widget;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import pl.edu.agh.winmon.hibernate.Session;
import pl.edu.agh.winmon.hibernate.Turn;

/**
 * This is a waidget which allows to choose date of the begin and end of sth.
 * 
 * @author kret
 * 
 */
public class TimePickerWidget extends Composite {

	private TimeCombo startDateCombo;

	private TimeCombo endDateCombo;

	private static Long startDate;

	private static Long endDate;

	private Collection<SelectionListener> listeners = new HashSet<SelectionListener>();

	private static Collection<TimePickerWidget> timePickerWidgets = new HashSet<TimePickerWidget>();

	private static void repaintAll() {
		for (TimePickerWidget tpw : timePickerWidgets) {
			tpw.redraw();
		}
	}

	public TimePickerWidget(Composite parent) {
		super(parent, SWT.NONE);

		setLayout(new GridLayout(4, false));

		Label startDateLabel = new Label(this, SWT.NONE);
		startDateLabel.setText("Start date");

		startDateCombo = new TimeCombo(this, SWT.NONE);
		startDateCombo.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				startDate = startDateCombo.getDate().getTime();
				repaintAll();
			}

		});

		Label endDateLabel = new Label(this, SWT.NONE);
		endDateLabel.setText("End date");

		endDateCombo = new TimeCombo(this, SWT.NONE);
		
		endDateCombo.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				endDate = endDateCombo.getDate().getTime();
				repaintAll();
			}

		});

		timePickerWidgets.add(this);
	}

	@Override
	public void redraw() {
		Date date = new Date(startDate);
		startDateCombo.setDate(date);

		date = new Date(endDate);
		endDateCombo.setDate(date);

		// TODO Add specific listener type
		for (SelectionListener sl : listeners) {
			sl.widgetSelected(null);
		}

		super.redraw();
	}

	public void setSession(Session session) {
		startDateCombo.removeAll();
		endDateCombo.removeAll();
		for (Turn turn : session.getTurns()) {
			if (turn != null) {
				startDateCombo.addDate(new Date(turn.getStartTime()));
			}
		}

		for (Turn turn : session.getTurns()) {
			if (turn != null) {
				endDateCombo.addDate(new Date(turn.getStartTime()));
			}
		}

		startDateCombo.select(0);
		startDate = (Long) startDateCombo.getDate().getTime();
		endDateCombo.select(endDateCombo.getItemCount() - 1);
		endDate = (Long) endDateCombo.getDate().getTime();

		startDateCombo.getParent().getParent().layout();
		layout();
	}

	public long getStartDate() {
		return startDate;
	}

	public long getEndDate() {
		return endDate;
	}

	public void addSelectionListener(SelectionListener listener) {
		listeners.add(listener);
		startDateCombo.addSelectionListener(listener);
		endDateCombo.addSelectionListener(listener);
	}
	
	public TimeCombo getStartTimeCombo() {
		return startDateCombo;
	}
	
	public TimeCombo getEndTimeCombo() {
		return endDateCombo;
	}
}
